package edu.wfu.preparedDishes.commodity.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import edu.wfu.preparedDishes.commodity.entity.CommoditySpuEntity;
import edu.wfu.preparedDishes.commodity.entity.commodityVo;
import edu.wfu.preparedDishes.commodity.service.CommoditySpuService;
import edu.wfu.preparedDishes.common.utils.PageUtils;
import edu.wfu.preparedDishes.common.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;


/**
 * 商品spu表;
 *
 * @author hero_miracle
 * @email hero_miracle@gmail.com
 * @date 2023-11-21 19:21:25
 */
@RestController
@RequestMapping("commodity/commodityspu")
public class CommoditySpuController {
    @Autowired
    private CommoditySpuService commoditySpuService;




    /**
     * 查询菜品
     * @param pageSize
     * @param page
     * @param orderBy
     * @param merchantId
     * @return
     */
    @RequestMapping("/commodities")
    public List<commodityVo> getCommodities(
            @RequestParam(name = "pageSize", defaultValue = "5") int pageSize,
            @RequestParam(name = "page", defaultValue = "0") int page,
            @RequestParam(name = "orderBy", defaultValue = "") String orderBy,
            @RequestParam(name = "merchantId", defaultValue = "") String merchantId,
            @RequestParam(name = "spuNameLike", defaultValue = "") String spuNameLike,
            @RequestParam(name = "classification", defaultValue = "") String classification,
            @RequestParam(name = "spuId", defaultValue = "") String spuId, HttpServletRequest request)
    {
        int offset = page * pageSize;
        return commoditySpuService.queryCommodities(pageSize, offset, orderBy,merchantId,spuNameLike,classification,spuId);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{spuId}")
    public R info(@PathVariable("spuId") String spuId){
		CommoditySpuEntity commoditySpu = commoditySpuService.getById(spuId);
        return R.ok().put("commoditySpu", commoditySpu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CommoditySpuEntity commoditySpu){
		commoditySpuService.save(commoditySpu);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CommoditySpuEntity commoditySpu){
		commoditySpuService.updateById(commoditySpu);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] spuIds){
		commoditySpuService.removeByIds(Arrays.asList(spuIds));
        return R.ok();
    }

}
